package com.zhixi.pattern.important.proxypattern.JDK.operation;

import java.lang.reflect.Proxy;

/**
 * @作者 ShuHaoNan
 * @邮箱 shuhaonan1@gmail.com
 * @时间 2020/11/15 17:27
 */
public class GamePlayerProxy {

    private Object target;

    public GamePlayerProxy(Object target) {
        this.target = target;
    }

    public Object getProxyInstance() {
        return Proxy.newProxyInstance(
                this.target.getClass().getClassLoader(),
                this.target.getClass().getInterfaces(),
                (proxy, method, args) -> {
                    /* 动态判断方法实现 */
                    if (method.getName().equalsIgnoreCase("login")) {
                        System.out.println("给玩家发送一条消息，您的账户在某某某地方登录");
                    }
                    return method.invoke(this.target, args);
                });
    }

}
